package com.foreveredu.propms.web.struts.action;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.foreveredu.propms.domain.Account;
import com.foreveredu.propms.domain.Bill;
import com.foreveredu.propms.domain.BillOther;
import com.foreveredu.propms.domain.ChargeRecord;
import com.foreveredu.propms.domain.District;
import com.foreveredu.propms.domain.Exchange;
import com.foreveredu.propms.domain.Owner;
import com.foreveredu.propms.domain.SubAccount;
import com.foreveredu.propms.domain.SubDistrict;
import com.foreveredu.propms.domain.User;
import com.foreveredu.propms.domain.Utilities;
import com.foreveredu.propms.service.IBusinessService;
import com.foreveredu.propms.service.IChargeService;
import com.foreveredu.propms.service.IManagementService;
import com.foreveredu.propms.web.struts.actionform.BillActionForm;
import com.foreveredu.propms.web.util.DigitalUpperCase;

/**
 * 处理账单请求
 * 
 * @author hushigang
 * 
 */
public class BillAction extends AbstractBaseDispatchAction {

	private IBusinessService businessService = null;
	private IManagementService managementService = null;
	private IChargeService chargeService = null;

	// 获得日志记录器
	Logger log = Logger.getLogger(BillAction.class);

	public IManagementService getManagementService() {
		return managementService;
	}

	public void setManagementService(IManagementService managementService) {
		this.managementService = managementService;
	}

	public IBusinessService getBusinessService() {
		return businessService;
	}

	public void setBusinessService(IBusinessService businessService) {
		this.businessService = businessService;
	}

	public IChargeService getChargeService() {
		return chargeService;
	}

	public void setChargeService(IChargeService chargeService) {
		this.chargeService = chargeService;
	}

	// 跳转到选择小区的页面
	public ActionForward chooseSub(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		// 获取当前登录用户
		User user = null;
		try {
			user = managementService.findUserByUsername(this.getPrincipal()
					.getUsername());
		} catch (ClassCastException e) {
			// 用户长时间不操作，session丢失后，跳转到登录页面
			return mapping.findForward("login");
		}

		// 获取当前物业管理员所在的物业服务处
		District dis = user.getDistrict();
		log.info("dis.name=" + dis.getName());
		request.setAttribute("dis", dis);

		List<SubDistrict> subList = managementService
				.findSubDistrictsOfDistrict(dis);
		request.setAttribute("subList", subList);

		return mapping.findForward("chooseSub");
	}

	// 为小区中的每个房产生成本月账单,即系统计费
	public ActionForward create(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		BillActionForm baf = (BillActionForm) form;
		SubDistrict sub = managementService.findSubDistrictById(baf.getSubId());

		try {
			businessService.createBillForHouseOfSubDistrict(sub,
					baf.getBillYear(), baf.getBillMonth());
		} catch (Exception e) {
			// 系统计费报错
			request.setAttribute("error", "系统计费出错，请检查是否设置相应的收费标准！");
		}

		return mapping.findForward("createDone");
	}

	// 跳转到业主账单付费页面
	public ActionForward pay(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		BillActionForm baf = (BillActionForm) form;
		Bill bill = businessService.findBillById(baf.getId());
		request.setAttribute("bill", bill);

		// 是否已预收物业服务费
		int isPreCharged = baf.getIsPreCharged();
		float wantMoney = baf.getWantMoney();// 本月应缴纳物业服务费金额
		if (isPreCharged == 1 && wantMoney > 0) {
			float realWantMoney = Float.parseFloat(new DecimalFormat("#.0")
					.format(bill.getAmount() - wantMoney));
			request.setAttribute("realWantMoney", realWantMoney);
		}
		request.setAttribute("isPreCharged", isPreCharged);
		request.setAttribute("wantMoney", wantMoney);

		int isHistoryBill = 0;
		if (bill.getYear() == 0 && bill.getMonth() == 0) {
			isHistoryBill = 1;
		}
		request.setAttribute("isHistoryBill", isHistoryBill);

		return mapping.findForward("billPay");
	}

	// 跳转到合并支付页面
	public ActionForward mergePay(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		// 获得合并支付的所有账单ID
		String ids = request.getParameter("ids");
		log.info("ids=" + ids);
		String[] billIds = ids.split(",");
		if (billIds != null) {
			Owner owner = businessService
					.findBillById(Integer.parseInt(billIds[0])).getHouse()
					.getOwner();
			request.setAttribute("owner", owner);
		}

		// 获得各账单信息，计算总金额
		float mergePayAmount = 0;
		for (int i = 0; i < billIds.length; i++) {
			Bill bill = businessService.findBillById(Integer
					.parseInt(billIds[i]));
			mergePayAmount += bill.getAmount();
		}
		request.setAttribute("mergePayAmount", mergePayAmount);

		return mapping.findForward("mergePay");
	}

	// 业主完成账单现金付费操作
	public ActionForward paySave(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		// 获得表单数据
		BillActionForm baf = (BillActionForm) form;
		Bill bill = businessService.findBillById(baf.getId());

		// 获得当前日期
		String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
				.format(new Date());

		// 判断业主是否将零钱存入账户
		float smallChange = baf.getPaidAmount()
				- (bill.getAmount() + baf.getChange());

		if (smallChange > 0) {
			Account account = businessService.findAccountByOwner(bill
					.getHouse().getOwner());
			// 判断该业主账户是否存在,如果不存在则为此业主创建一个账户,并将零钱存入此账户
			if (account == null) {
				account = new Account();
				account.setOwner(bill.getHouse().getOwner());
				account.setBalance(smallChange);
				account.setLastUsedDate(date);
				businessService.saveAccount(account);

				// 创建预存费用子账户
				SubAccount subAccount = new SubAccount();
				subAccount.setAccount(account);
				subAccount.setType(5);
				subAccount.setBalance(smallChange);
				businessService.saveSubAccount(subAccount);

			} else {
				// 修改主账户余额
				account.setBalance(account.getBalance() + smallChange);
				account.setLastUsedDate(date);
				businessService.updateAccount(account);

				// 获取预存费用子账户，并修改其余额
				SubAccount subAccount = businessService.findSubAccountByType(
						account, 5);
				if (subAccount != null) {
					subAccount
							.setBalance(subAccount.getBalance() + smallChange);
					businessService.updateSubAccount(subAccount);
				} else {
					// 创建预存费用子账户
					SubAccount sub = new SubAccount();
					sub.setAccount(account);
					sub.setType(5);
					sub.setBalance(smallChange);
					businessService.saveSubAccount(sub);
				}
			}
			Exchange exchange = new Exchange();
			exchange.setAccount(account);
			exchange.setBalance(account.getBalance());
			exchange.setDeposit(smallChange);
			exchange.setExtime(date);
			businessService.saveExchange(exchange);
		}

		/*
		 * 如果已预收物业服务费，则从业主子账户扣除相应费用
		 */
		float wantMoney = 0;
		int isPreCharged = baf.getIsPreCharged();
		if (isPreCharged == 1) {
			wantMoney = baf.getWantMoney();// 当月应缴物业服务费

			// 查询该房产的业主是否在账户中预存物业服务费，并检查物业服务费子账户余额是否足够支付当前物业服务费金额
			Account account = businessService.findAccountByOwner(bill
					.getHouse().getOwner());
			if (account != null) {
				SubAccount subAccount = businessService.findSubAccountByType(
						account, 1);
				if (subAccount != null) {

					// 从业主物业服务费子账户扣除当前物业服务费金额
					float newBalance = Float
							.parseFloat(new DecimalFormat("#.00")
									.format(subAccount.getBalance() - wantMoney));
					subAccount.setBalance(newBalance);
					businessService.updateSubAccount(subAccount);

					// 从业主主账户扣除当前物业服务费金额
					float newBalance2 = Float.parseFloat(new DecimalFormat(
							"#.00").format(account.getBalance() - wantMoney));
					account.setBalance(newBalance2);
					account.setLastUsedDate(date);
					businessService.updateAccount(account);

					// 保存账户交易记录
					Exchange exchange = new Exchange();
					exchange.setAccount(account);
					exchange.setBalance(account.getBalance());
					exchange.setWithdraw(wantMoney);
					exchange.setExtime(date);
					businessService.saveExchange(exchange);
				}
			}
		}

		// 设置账单实付金额
		bill.setPaidAmount(Float.parseFloat(new DecimalFormat("0.0").format(baf
				.getPaidAmount() - baf.getChange() + wantMoney)));
		bill.setIsPaid(1);// 设置账单为“已付”状态
		businessService.updateBill(bill);

		// 预存金额
		if (smallChange < 0) {
			request.setAttribute("smallChange", 0);
		} else {
			request.setAttribute("smallChange", smallChange);
		}

		// 预缴金额
		request.setAttribute("preChargedMoney", wantMoney);

		// 本次付费
		request.setAttribute(
				"thisPaidAmount",
				Float.parseFloat(new DecimalFormat("#.0").format(bill
						.getAmount() - wantMoney)));

		request.setAttribute("bill", bill);
		request.setAttribute("amountUpperCase",
				DigitalUpperCase.toUpperCase(bill.getAmount()));

		// 获得该房产账单中的收费记录列表
		List<ChargeRecord> recordList = businessService
				.findChargeRecordsByBill(bill);
		request.setAttribute("recordList", recordList);

		int isHistoryBill = 1;// 历史账单
		if (bill.getYear() != 0 && bill.getMonth() != 0) {
			isHistoryBill = 0;// 非历史账单
			request.setAttribute("isHistoryBill", isHistoryBill);

			// 获得该房产本月水电使用量
			Utilities utilities = businessService.findUtilitiesByHouseAndDate(
					bill.getHouse(), bill.getYear(), bill.getMonth());
			float powerBegin = utilities.getPowerBegin();
			float powerEnd = utilities.getPowerEnd();
			float powerVolume = powerEnd - powerBegin;

			float power2Begin = utilities.getPower2Begin();
			float power2End = utilities.getPower2End();
			float power2Volume = power2End - power2Begin;

			float waterBegin = utilities.getWaterBegin();
			float waterEnd = utilities.getWaterEnd();
			float waterVolume = waterEnd - waterBegin;

			float water2Begin = utilities.getWater2Begin();
			float water2End = utilities.getWater2End();
			float water2Volume = water2End - water2Begin;

			request.setAttribute("powerBegin", (int) powerBegin);
			request.setAttribute("powerEnd", (int) powerEnd);
			request.setAttribute("waterBegin", (int) waterBegin);
			request.setAttribute("waterEnd", (int) waterEnd);
			request.setAttribute("powerVolume", (int) powerVolume);
			request.setAttribute("waterVolume", (int) waterVolume);

			request.setAttribute("power2Begin", (int) power2Begin);
			request.setAttribute("power2End", (int) power2End);
			request.setAttribute("water2Begin", (int) water2Begin);
			request.setAttribute("water2End", (int) water2End);
			request.setAttribute("power2Volume", (int) power2Volume);
			request.setAttribute("water2Volume", (int) water2Volume);
		}

		// 历史欠款：
		float historyDebt = businessService.findHistoryDebtOfHouse(bill
				.getHouse());
		request.setAttribute("historyDebt",
				Float.parseFloat(new DecimalFormat("0.00").format(historyDebt)));

		// 违约金：

		// 收费员 /抄表员 /开票员：当前登录用户
		User user = null;
		try {
			user = managementService.findUserByUsername(this.getPrincipal()
					.getUsername());
		} catch (ClassCastException e) {
			// 用户长时间不操作，session丢失后，跳转到登录页面
			return mapping.findForward("login");
		}
		request.setAttribute("nickname", user.getNickname());

		// 开票日期：
		request.setAttribute("invoiceDate",
				new SimpleDateFormat("yyyy-MM-dd").format(new Date()));

		// 账户余额
		Account account = businessService.findAccountByOwner(bill.getHouse()
				.getOwner());
		request.setAttribute("balance",
				account == null ? 0 : account.getBalance());

		return mapping.findForward("billPaySave");
	}

	// 业主完成账单现金付费操作
	public ActionForward mergePaySave(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		// 获得表单数据
		BillActionForm baf = (BillActionForm) form;
		String ids = baf.getIds();
		String[] billIds = ids.split(",");

		Bill bill = businessService.findBillById(baf.getId());

		// 获得当前日期
		String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
				.format(new Date());

		// 判断业主是否将零钱存入账户
		float smallChange = baf.getPaidAmount()
				- (bill.getAmount() + baf.getChange());

		if (smallChange > 0) {
			Account account = businessService.findAccountByOwner(bill
					.getHouse().getOwner());
			// 判断该业主账户是否存在,如果不存在则为此业主创建一个账户,并将零钱存入此账户
			if (account == null) {
				account = new Account();
				account.setOwner(bill.getHouse().getOwner());
				account.setBalance(smallChange);
				account.setLastUsedDate(date);
				businessService.saveAccount(account);

				// 创建预存费用子账户
				SubAccount subAccount = new SubAccount();
				subAccount.setAccount(account);
				subAccount.setType(5);
				subAccount.setBalance(smallChange);
				businessService.saveSubAccount(subAccount);

			} else {
				// 修改主账户余额
				account.setBalance(account.getBalance() + smallChange);
				account.setLastUsedDate(date);
				businessService.updateAccount(account);

				// 获取预存费用子账户，并修改其余额
				SubAccount subAccount = businessService.findSubAccountByType(
						account, 5);
				if (subAccount != null) {
					subAccount
							.setBalance(subAccount.getBalance() + smallChange);
					businessService.updateSubAccount(subAccount);
				} else {
					// 创建预存费用子账户
					SubAccount sub = new SubAccount();
					sub.setAccount(account);
					sub.setType(5);
					sub.setBalance(smallChange);
					businessService.saveSubAccount(sub);
				}
			}
			Exchange exchange = new Exchange();
			exchange.setAccount(account);
			exchange.setBalance(account.getBalance());
			exchange.setDeposit(smallChange);
			exchange.setExtime(date);
			businessService.saveExchange(exchange);
		}

		/*
		 * 如果已预收物业服务费，则从业主子账户扣除相应费用
		 */
		float wantMoney = 0;
		int isPreCharged = baf.getIsPreCharged();
		if (isPreCharged == 1) {
			wantMoney = baf.getWantMoney();// 当月应缴物业服务费

			// 查询该房产的业主是否在账户中预存物业服务费，并检查物业服务费子账户余额是否足够支付当前物业服务费金额
			Account account = businessService.findAccountByOwner(bill
					.getHouse().getOwner());
			if (account != null) {
				SubAccount subAccount = businessService.findSubAccountByType(
						account, 1);
				if (subAccount != null) {

					// 从业主物业服务费子账户扣除当前物业服务费金额
					float newBalance = Float
							.parseFloat(new DecimalFormat("#.00")
									.format(subAccount.getBalance() - wantMoney));
					subAccount.setBalance(newBalance);
					businessService.updateSubAccount(subAccount);

					// 从业主主账户扣除当前物业服务费金额
					float newBalance2 = Float.parseFloat(new DecimalFormat(
							"#.00").format(account.getBalance() - wantMoney));
					account.setBalance(newBalance2);
					account.setLastUsedDate(date);
					businessService.updateAccount(account);

					// 保存账户交易记录
					Exchange exchange = new Exchange();
					exchange.setAccount(account);
					exchange.setBalance(account.getBalance());
					exchange.setWithdraw(wantMoney);
					exchange.setExtime(date);
					businessService.saveExchange(exchange);
				}
			}
		}

		// 设置账单实付金额
		bill.setPaidAmount(Float.parseFloat(new DecimalFormat("0.0").format(baf
				.getPaidAmount() - baf.getChange() + wantMoney)));
		bill.setIsPaid(1);// 设置账单为“已付”状态
		businessService.updateBill(bill);

		// 预存金额
		if (smallChange < 0) {
			request.setAttribute("smallChange", 0);
		} else {
			request.setAttribute("smallChange", smallChange);
		}

		// 预缴金额
		request.setAttribute("preChargedMoney", wantMoney);

		// 本次付费
		request.setAttribute(
				"thisPaidAmount",
				Float.parseFloat(new DecimalFormat("#.0").format(bill
						.getAmount() - wantMoney)));

		request.setAttribute("bill", bill);
		request.setAttribute("amountUpperCase",
				DigitalUpperCase.toUpperCase(bill.getAmount()));

		// 获得该房产账单中的收费记录列表
		List<ChargeRecord> recordList = businessService
				.findChargeRecordsByBill(bill);
		request.setAttribute("recordList", recordList);

		int isHistoryBill = 1;// 历史账单
		if (bill.getYear() != 0 && bill.getMonth() != 0) {
			isHistoryBill = 0;// 非历史账单
			request.setAttribute("isHistoryBill", isHistoryBill);

			// 获得该房产本月水电使用量
			Utilities utilities = businessService.findUtilitiesByHouseAndDate(
					bill.getHouse(), bill.getYear(), bill.getMonth());
			float powerBegin = utilities.getPowerBegin();
			float powerEnd = utilities.getPowerEnd();
			float powerVolume = powerEnd - powerBegin;

			float power2Begin = utilities.getPower2Begin();
			float power2End = utilities.getPower2End();
			float power2Volume = power2End - power2Begin;

			float waterBegin = utilities.getWaterBegin();
			float waterEnd = utilities.getWaterEnd();
			float waterVolume = waterEnd - waterBegin;

			float water2Begin = utilities.getWater2Begin();
			float water2End = utilities.getWater2End();
			float water2Volume = water2End - water2Begin;

			request.setAttribute("powerBegin", (int) powerBegin);
			request.setAttribute("powerEnd", (int) powerEnd);
			request.setAttribute("waterBegin", (int) waterBegin);
			request.setAttribute("waterEnd", (int) waterEnd);
			request.setAttribute("powerVolume", (int) powerVolume);
			request.setAttribute("waterVolume", (int) waterVolume);

			request.setAttribute("power2Begin", (int) power2Begin);
			request.setAttribute("power2End", (int) power2End);
			request.setAttribute("water2Begin", (int) water2Begin);
			request.setAttribute("water2End", (int) water2End);
			request.setAttribute("power2Volume", (int) power2Volume);
			request.setAttribute("water2Volume", (int) water2Volume);
		}

		// 历史欠款：
		float historyDebt = businessService.findHistoryDebtOfHouse(bill
				.getHouse());
		request.setAttribute("historyDebt",
				Float.parseFloat(new DecimalFormat("0.00").format(historyDebt)));

		// 违约金：

		// 收费员 /抄表员 /开票员：当前登录用户
		User user = null;
		try {
			user = managementService.findUserByUsername(this.getPrincipal()
					.getUsername());
		} catch (ClassCastException e) {
			// 用户长时间不操作，session丢失后，跳转到登录页面
			return mapping.findForward("login");
		}
		request.setAttribute("nickname", user.getNickname());

		// 开票日期：
		request.setAttribute("invoiceDate",
				new SimpleDateFormat("yyyy-MM-dd").format(new Date()));

		// 账户余额
		Account account = businessService.findAccountByOwner(bill.getHouse()
				.getOwner());
		request.setAttribute("balance",
				account == null ? 0 : account.getBalance());

		return mapping.findForward("billPaySave");
	}

	// 业主完成账单账户付费操作
	public ActionForward accountPaySave(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		// 获得表单数据
		BillActionForm baf = (BillActionForm) form;
		Bill bill = businessService.findBillById(baf.getId());

		// 获得当前日期
		String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
				.format(new Date());

		/*
		 * 如果已预收物业服务费，则从业主子账户扣除相应费用
		 */
		float wantMoney = 0;
		int isPreCharged = baf.getIsPreCharged();
		if (isPreCharged == 1) {
			wantMoney = baf.getWantMoney();// 当月应缴物业服务费

			// 查询该房产的业主是否在账户中预存物业服务费，并检查物业服务费子账户余额是否足够支付当前物业服务费金额
			Account account = businessService.findAccountByOwner(bill
					.getHouse().getOwner());
			if (account != null) {
				SubAccount subAccount = businessService.findSubAccountByType(
						account, 1);
				if (subAccount != null) {

					// 从业主物业服务费子账户扣除当前物业服务费金额
					float newBalance = Float
							.parseFloat(new DecimalFormat("#.00")
									.format(subAccount.getBalance() - wantMoney));
					subAccount.setBalance(newBalance);
					businessService.updateSubAccount(subAccount);

					// 从业主主账户扣除当前物业服务费金额
					float newBalance2 = Float.parseFloat(new DecimalFormat(
							"#.00").format(account.getBalance() - wantMoney));
					account.setBalance(newBalance2);
					businessService.updateAccount(account);

					// 保存账户交易记录
					Exchange exchange = new Exchange();
					exchange.setAccount(account);
					exchange.setBalance(account.getBalance());
					exchange.setWithdraw(wantMoney);
					exchange.setExtime(date);
					businessService.saveExchange(exchange);
				}
			}
		}
		Account account = businessService.findAccountByOwner(bill.getHouse()
				.getOwner());

		// 判断该业主账户是否存在
		if (account == null) {
			request.setAttribute("bill", bill);
			request.setAttribute("accountNotFound", "该业主目前还没有开通账户！");
			return mapping.findForward("billPay");
		} else {
			// 获得预存费用子账户金额
			SubAccount subAccount = businessService.findSubAccountByType(
					account, 5);
			if (subAccount == null) {
				request.setAttribute("bill", bill);
				request.setAttribute("depositSubAccountNotOpen",
						"该业主的预存费用子账户没有开通！");
				return mapping.findForward("billPay");
			}
			if (subAccount.getBalance() < (bill.getAmount() - wantMoney)) {
				request.setAttribute("bill", bill);
				request.setAttribute("accountBalanceInsufficient", "该业主账户余额不足！");
				return mapping.findForward("billPay");
			}
			// 更新子账户金额
			subAccount.setBalance(Float.parseFloat(new DecimalFormat("#.00")
					.format(subAccount.getBalance()
							- (bill.getAmount() - wantMoney))));
			businessService.updateSubAccount(subAccount);

			// 更新主账户金额
			account.setBalance(Float.parseFloat(new DecimalFormat("#.00")
					.format(subAccount.getBalance()
							- (bill.getAmount() - wantMoney))));
			account.setLastUsedDate(date);
			businessService.updateAccount(account);

			Exchange exchange = new Exchange();
			exchange.setAccount(account);
			exchange.setWithdraw(Float.parseFloat(new DecimalFormat("#.00")
					.format(bill.getAmount() - wantMoney)));
			exchange.setBalance(account.getBalance());
			exchange.setExtime(date);
			businessService.saveExchange(exchange);
		}

		// 设置账单实付金额
		bill.setPaidAmount(bill.getAmount());
		bill.setIsPaid(1);// 设置账单为“已付”状态
		businessService.updateBill(bill);

		// 预存金额
		request.setAttribute("smallChange", 0);

		// 预缴金额
		request.setAttribute("preChargedMoney", wantMoney);

		// 本次付费
		request.setAttribute(
				"thisPaidAmount",
				Float.parseFloat(new DecimalFormat("#.0").format(bill
						.getAmount() - wantMoney)));

		request.setAttribute("bill", bill);
		request.setAttribute("amountUpperCase",
				DigitalUpperCase.toUpperCase(bill.getAmount()));

		// 获得该房产账单中的收费记录列表
		List<ChargeRecord> recordList = businessService
				.findChargeRecordsByBill(bill);
		request.setAttribute("recordList", recordList);

		int isHistoryBill = 1;// 历史账单
		if (bill.getYear() != 0 && bill.getMonth() != 0) {
			isHistoryBill = 0;// 非历史账单
			request.setAttribute("isHistoryBill", isHistoryBill);

			// 获得该房产本月水电使用量
			Utilities utilities = businessService.findUtilitiesByHouseAndDate(
					bill.getHouse(), bill.getYear(), bill.getMonth());
			float powerBegin = utilities.getPowerBegin();
			float powerEnd = utilities.getPowerEnd();
			float powerVolume = powerEnd - powerBegin;

			float power2Begin = utilities.getPower2Begin();
			float power2End = utilities.getPower2End();
			float power2Volume = power2End - power2Begin;

			float waterBegin = utilities.getWaterBegin();
			float waterEnd = utilities.getWaterEnd();
			float waterVolume = waterEnd - waterBegin;

			float water2Begin = utilities.getWater2Begin();
			float water2End = utilities.getWater2End();
			float water2Volume = water2End - water2Begin;

			request.setAttribute("powerBegin", (int) powerBegin);
			request.setAttribute("powerEnd", (int) powerEnd);
			request.setAttribute("waterBegin", (int) waterBegin);
			request.setAttribute("waterEnd", (int) waterEnd);
			request.setAttribute("powerVolume", (int) powerVolume);
			request.setAttribute("waterVolume", (int) waterVolume);

			request.setAttribute("power2Begin", (int) power2Begin);
			request.setAttribute("power2End", (int) power2End);
			request.setAttribute("water2Begin", (int) water2Begin);
			request.setAttribute("water2End", (int) water2End);
			request.setAttribute("power2Volume", (int) power2Volume);
			request.setAttribute("water2Volume", (int) water2Volume);
		}

		// 历史欠款：
		float historyDebt = businessService.findHistoryDebtOfHouse(bill
				.getHouse());
		request.setAttribute("historyDebt",
				Float.parseFloat(new DecimalFormat("0.00").format(historyDebt)));

		// 违约金：

		// 收费员 /抄表员 /开票员：当前登录用户
		User user = null;
		try {
			user = managementService.findUserByUsername(this.getPrincipal()
					.getUsername());
		} catch (ClassCastException e) {
			// 用户长时间不操作，session丢失后，跳转到登录页面
			return mapping.findForward("login");
		}
		request.setAttribute("nickname", user.getNickname());

		// 开票日期：
		request.setAttribute("invoiceDate",
				new SimpleDateFormat("yyyy-MM-dd").format(new Date()));

		// 账户余额
		request.setAttribute("balance", account.getBalance());

		return mapping.findForward("billPaySave");
	}

	// 删除重复bill
	public ActionForward deleteRepeatBill(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
//		try {
			List<BillOther> billOthers = businessService.findBillOthers();
			for (Iterator<BillOther> billOtherIterator = billOthers.iterator(); billOtherIterator
					.hasNext();) {
				BillOther billOther = billOtherIterator.next();
				List<Bill> bills = businessService.findBillsByBillOther(billOther);
				int size = bills.size();
				for (int n = 1; size - 1 >= n; n++) {
					Bill bill = bills.get(n);
//					List<ChargeRecord> chargeRecords = businessService.findChargeRecordsByBill(bill);
//					for (Iterator<ChargeRecord> chargeRecordIterator = chargeRecords
//							.iterator(); chargeRecordIterator.hasNext();) {
//						ChargeRecord chargeRecord = chargeRecordIterator.next();
//						if (chargeRecord != null || !"".equals(chargeRecord)) {
//							chargeService.deleteChargeRecordById(chargeRecord
//									.getId());
//						}
//					}
					businessService.deleteBillById(bill.getId());
				}

			}

//		} catch (Exception e) {
//			return mapping.findForward("deleteRepeatBill");
//		}
		return null;
	}
}
